import { VideoPlayer } from 'cc';
import { Observable, Observer } from 'rxjs';

export function VideoPlayerPlay(videoPlayer: VideoPlayer): Observable<any> {
    return new Observable((observer: Observer<{ state: string }>) => {
        let eventFinished = () => {
            observer.next({ state: VideoPlayer.EventType.COMPLETED });
            observer.complete();
        };
        videoPlayer.node.on(VideoPlayer.EventType.COMPLETED, eventFinished);
        videoPlayer.play();
        return () => {
            if (videoPlayer.isPlaying) videoPlayer.stop();
            videoPlayer.node.off(VideoPlayer.EventType.COMPLETED, eventFinished);
        }
    });
}